# coding:utf-8
from odoo import models, fields, api, _


class OLResPartner(models.Model):
    _name = 'res.partner'
    _inherit = 'res.partner'

    property_product_pricelist_ids = fields.Many2many('product.pricelist', 'partner_price_list_rel',
                                                      'price_list_id', 'partner_id', string='Price List Set')

    customer_payment_term_ids = fields.Many2many('account.payment.term', 'partner_customer_payment_term_rel',
                                                 'payment_id', 'partner_id', string='Customer Payment Terms')
    vendor_payment_term_ids = fields.Many2many('account.payment.term', 'partner_supplier_payment_term_rel',
                                               'payment_id', 'partner_id', string='Vendor Payment Terms')

    @api.onchange('customer_payment_term_ids', 'vendor_payment_term_ids')
    def _check_not_exist_pricelist(self):
        if self.property_payment_term_id.id not in self.customer_payment_term_ids.mapped('id'):
            self.property_payment_term_id = False
        if self.property_supplier_payment_term_id.id not in self.vendor_payment_term_ids.mapped('id'):
            self.property_supplier_payment_term_id = False

    @api.onchange('property_product_pricelist_ids')
    def _check_not_exist_pricelist(self):
        if self.property_product_pricelist.id not in self.property_product_pricelist_ids.mapped('id'):
            self.property_product_pricelist = False
